package com.zg.datapush.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.common.core.R;
import com.zg.common.core.dao.datapush.entity.DpPushTask;
import com.zg.datapush.vo.DpPushTaskVo;
import com.zg.datapush.req.*;
import com.zg.datapush.service.DpPushTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 推送任务Controller
 * @author zg
 */
@RestController
@RequestMapping("/datapush/task")
@RequiredArgsConstructor
@Api(tags = "推送任务管理")
public class DpPushTaskController {

    private final DpPushTaskService pushTaskService;

    @ApiOperation("分页查询推送任务")
    @PostMapping("/page")
    public R<IPage<DpPushTaskVo>> page(@RequestBody @Valid DpPushTaskPageReq req) {
        Page<DpPushTask> page = new Page<>(req.getPage(), req.getPageSize());
        IPage<DpPushTaskVo> result = pushTaskService.page(page, req);
        return R.success(result);
    }

    @ApiOperation("新增推送任务")
    @PostMapping("/add")
    public R<DpPushTask> add(@RequestBody @Valid DpPushTaskAddReq req) {
        return pushTaskService.add(req);
    }

    @ApiOperation("更新推送任务")
    @PostMapping("/update")
    public R<DpPushTask> update(@RequestBody @Valid DpPushTaskUpdateReq req) {
        return pushTaskService.update(req);
    }

    @ApiOperation("删除推送任务")
    @PostMapping("/delete")
    public R<Void> delete(@RequestBody @Valid DpPushTaskIdsReq req) {
        return pushTaskService.delete(req.getIds());
    }

    @ApiOperation("启用推送任务")
    @PostMapping("/enable")
    public R<Void> enable(@RequestBody @Valid DpPushTaskIdReq req) {
        return pushTaskService.enable(req.getId());
    }

    @ApiOperation("禁用推送任务")
    @PostMapping("/disable")
    public R<Void> disable(@RequestBody @Valid DpPushTaskIdReq req) {
        return pushTaskService.disable(req.getId());
    }

    @ApiOperation("手动执行推送任务")
    @PostMapping("/execute")
    public R<Void> execute(@RequestBody @Valid DpPushTaskIdReq req) {
        return pushTaskService.execute(req.getId());
    }

    @ApiOperation("启动任务调度")
    @PostMapping("/start-schedule")
    public R<Void> startSchedule(@RequestBody @Valid DpPushTaskIdReq req) {
        return pushTaskService.startSchedule(req.getId());
    }

    @ApiOperation("停止任务调度")
    @PostMapping("/stop-schedule")
    public R<Void> stopSchedule(@RequestBody @Valid DpPushTaskIdReq req) {
        return pushTaskService.stopSchedule(req.getId());
    }
}
